<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="申请人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题或事由" prop="sealTitle">
        <el-input
          v-model="queryParams.sealTitle"
          placeholder="请输入标题或事由"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['seal:jySeal:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['seal:jySeal:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['seal:jySeal:remove']"
        >取消
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['seal:jySeal:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jySealList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="简单用印流水号" align="center" prop="sealId"/>
      <el-table-column label="申请人姓名" align="center" prop="userName"/>
      <el-table-column label="申请人部门" align="center" prop="userDept"/>
      <el-table-column label="申请印章类别" align="center" prop="sealType"/>
      <el-table-column label="申请日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题或事由" align="center" prop="sealTitle"/>
      <el-table-column label="状态" align="center" prop="deleted">
        <template slot-scope="{row}">
          <el-tag
            type="success"
            v-show="row.deleted===0"
          >
           正常
          </el-tag>
          <el-tag
            type="danger"
            v-show="row.deleted===1"
          > 已取消
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['seal:jySeal:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['seal:jySeal:remove']"
          >取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用印对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="18" :offset="1">
            <el-form-item label="申请人姓名" prop="userName">
              <el-input :disabled="true" v-model="form.userName" placeholder="请输入申请人姓名"/>
            </el-form-item>
            <el-form-item label="申请人部门" prop="userDept">
              <el-input :disabled="true" dashed v-model="form.userDept" placeholder="请输入申请人部门"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18" :offset="1">
            <el-form-item label="用印类型:" prop="sealType">
              <el-input v-model="form.sealType" placeholder="请输入用印类型"/>
            </el-form-item>

            <el-form-item label="申请日期" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="标题或事由" prop="sealTitle">
              <el-input v-model="form.sealTitle" placeholder="请输入标题或事由"/>
            </el-form-item>
            <el-form-item label="上传附件" prop="sealFile">
              <file-upload :limit="10" :fileSize="10" v-model="form.sealFile"/>
            </el-form-item>
            <el-form-item label="文件份数" prop="fileNum">
              <el-input-number v-model="form.fileNum" :precision="0" controls-position="right" :min="0"
                               :step="1"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23" :offset="1">
            <el-form-item label="申请人备注" prop="userRemarks">
              <el-input v-model="form.userRemarks" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJySeal, getJySeal, delJySeal, addJySeal, updateJySeal, querySealType } from '@/api/seal/jySeal'

export default {
  name: 'JySeal',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 简单用印表格数据
      jySealList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        userDept: null,
        sealType: null,
        sealTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '申请人id不能为空', trigger: 'blur' }
        ]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '明天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() + 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          },
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }]
      }
    }
  },
  created() {
    this.getList()
  },
  //监听
  watch: {
    'form.sealFile': function(val) {
      if (val != null) {
        this.form.fileNum = val.split(',').length
      }
    }
  },
  methods: {
    /** 查询简单用印列表 */
    getList() {
      this.loading = true
      listJySeal(this.queryParams).then(response => {
        this.jySealList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sealId: null,
        userId: null,
        userName: null,
        userDept: null,
        sealType: null,
        createTime: null,
        sealTitle: null,
        sealFile: null,
        sealRemarks: null,
        deleted: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sealId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      querySealType().then(response => {
        this.form.userName = response.data.nickName
        this.form.userDept = response.data.deptName
      })
      this.reset()
      this.open = true
      this.title = '简单用印申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const sealId = row.sealId || this.ids
      getJySeal(sealId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改简单用印'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.sealId != null) {
            updateJySeal(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addJySeal(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 取消按钮操作 */
    handleDelete(row) {
      const sealIds = row.sealId || this.ids
      this.$modal.confirm('是否确认取消简单用印编号为"' + sealIds + '"的数据项？').then(function() {
        return delJySeal(sealIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('取消成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('seal/jySeal/export', {
        ...this.queryParams
      }, `jySeal_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
